package com.example.WebSocketDemo.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping(value = "/fileUpload")
public class FileUploadController {

    /**
     * 绑定文件上传路径到uploadPath
     */
    @Value("${web.upload-path}")
    private String uploadPath;

    @PostMapping("/image/{username}")
    public String imageUpload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request, @PathVariable String username) {
        File folder = new File(uploadPath+"/image/"+username);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        //System.out.println(uploadFile);
        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));
            System.out.println(request);
            // 返回上传文件的访问路径  http://localhost:2000//spring-master/85e4fcac-c903-4a7d-bdce-b155b4354402.jpeg
            return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath()
                    +"/"
                    + "image/"+username +"/" + newName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @PostMapping("/video/{username}")
    public String videoUpload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request, @PathVariable String username) {
        File folder = new File(uploadPath+"/video/"+username);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));

            // 返回上传文件的访问路径  http://localhost:2000//spring-master/85e4fcac-c903-4a7d-bdce-b155b4354402.jpeg
            return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath()
                    +"/"
                    + "video/"+username +"/" + newName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @PostMapping("/audio/{username}")
    public String audioUpload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request, @PathVariable String username) {
        File folder = new File(uploadPath+"/audio/"+username);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));

            return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath()
                    +"/"
                    + "audio/"+username +"/" + newName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @PostMapping("/other/{username}")
    public String otherUpload(@RequestParam("file") MultipartFile uploadFile, HttpServletRequest request,@PathVariable String username) {
        File folder = new File(uploadPath+"/other/"+username);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        // 对上传的文件重命名，避免文件重名
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID() + oldName.substring(oldName.lastIndexOf("."));
        try {
            // 文件保存
            uploadFile.transferTo(new File(folder, newName));

            return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                    + request.getContextPath()
                    +"/"
                    + "other/"+username +"/" + newName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}